import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

// 静态路由
const constRoutes = [
  {
    path: "/",
    redirect: "/home"
  },
  //登录
  {
    path: '/login',
    component: () => import('../views/login/Login.vue'),
  },
  //首页
  {
    path: '/home',
    meta: { title: '首页', menu: true },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '首页', menu: true },
        component: () => import('../views/home/Home.vue'),
      }
    ]
  },
]

// 动态路由
const dynamicRoutes = [
  {
    path: '/account',
    meta: { title: '账号管理', roles: ['super', 'normal'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/account/accountadd',
        meta: { title: '账号添加', roles: ['super', 'normal'] },
        component: () => import('../views/account/AccountAdd.vue'),
      },
      {
        path: '/account/accountlist',
        meta: { title: '账号列表', roles: ['super', 'normal'] },
        component: () => import('../views/account/AccountList.vue'),
      },
      {
        path: '/account/accountcenter',
        meta: { title: '个人中心', roles: ['super', 'normal'] },
        component: () => import('../views/account/AccountCenter.vue'),
      },
      {
        path: '/account/accountfix',
        meta: { title: '修改账号', roles: ['super', 'normal'] },
        component: () => import('../views/account/AaccountFix.vue'),
      },
    ]
  },
  {
    path: '/goods',
    meta: { title: '商品管理', roles: ['super', 'normal'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/goods/GoodsAdd',
        meta: { title: '商品添加', roles: ['super', 'normal'] },
        component: () => import('../views/goods/GoodsAdd.vue'),
      },
      {
        path: '/goods/GoodsList',
        meta: { title: '商品列表', roles: ['super', 'normal'] },
        component: () => import('../views/goods/GoodsList.vue'),
      },
      {
        path: '/goods/GoodsType',
        meta: { title: '商品分类', roles: ['super', 'normal'] },
        component: () => import('../views/goods/GoodsType.vue'),
      },
    ]
  },
  {
    path: '/order',
    meta: { title: '订单管理', roles: ['super', 'normal'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '订单管理', roles: ['super', 'normal'] },
        component: () => import('../views/order/Order.vue'),
      }
    ]
  },
  {
    path: '/shop',
    meta: { title: '店铺管理', roles: ['super', 'normal'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '店铺管理', roles: ['super', 'normal'] },
        component: () => import('../views/shop/Shop.vue'),
      }
    ]
  },
  {
    path: '/sale',
    meta: { title: '销售统计', roles: ['super', 'normal'] },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/sale/SaleShop',
        meta: { title: '商品统计', roles: ['super', 'normal'] },
        component: () => import('../views/sale/SaleShop.vue'),
      },
      {
        path: '/sale/Saleorder',
        meta: { title: '订单统计', roles: ['super', 'normal'] },
        component: () => import('../views/sale/Saleorder.vue'),
      },
    ]
  },
]




const router = new VueRouter({
  routes: constRoutes
})


function filterRoutes(routes, role) {
  return routes.filter(route => {
    if (route.children && route.children.length > 0) {
      route.children = filterRoutes(route.children, role)
    }
    return route.meta.roles.includes(role)
  })
}


export function addRouteByRole() {
  //拿到角色
  let role = localStorage.getItem('role')
  if (!role) return

  let roleArr = filterRoutes(dynamicRoutes, role)
  roleArr.forEach(route => {
    router.addRoute(route)
  })
  //404
  router.addRoute({
    path: '/*',
    component: () => import('@/views/test/Test.vue')
  })
}


// 根据角色得到菜单数据
export function getMenuListByRole() {
  const role = localStorage.getItem('role')
  if (!role) return []

  // 把静态路由里面打了标的route拿出来
  let arr = constRoutes.filter(route => route.meta && route.meta.menu)
  return [...arr, ...filterRoutes(dynamicRoutes, role)]
}

addRouteByRole()

//白名单（不登录也能访问，不设限制）
let whiteList = ['/login', '/register', '/test']
router.beforeEach((to, from, next) => {
  // 先判断to.fullPath在不在白名单里面，如果在白名单里，直接放行（）
  if (whiteList.includes(to.fullPath)) return next()
  let token = localStorage.getItem('token')
  if (token) {
    next()
  } else {
    next('/login')
  }
})




export default router
